// pages/home/home.js
const app = getApp()

Page({
  data: {
    adcode:'',
    city:'',
    humidity:'',
    province:'',
    reporttime:'',
    temperature:'',
    weather:'',
    winddirection:'',
    windpower:'',
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../index/index'
    })
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  onLoad:function(){
    var self = this;
    wx.request({
      url: 'https://restapi.amap.com/v3/weather/weatherInfo',
      data:{
        'key': 'd767da8249a56918c52694a1f1e96ccf',
        'city': '610100',
        'extensions': 'base'
      },
      header:{
        'content-type': 'application/json'
      },
      success:function(res){
        console.log(res.data);
        self.setData({
          city: res.data.lives[0].city,
          humidity: res.data.lives[0].humidity,
          province: res.data.lives[0].province,
          temperature: res.data.lives[0].temperature,
          reporttime: res.data.lives[0].reporttime,
          weather: res.data.lives[0].weather,
          winddirection: res.data.lives[0].winddirection,
          windpower: res.data.lives[0].windpower
        })
      }
    })
  }

})